<template>
  <div class="wrap_menu ivrwebcalldeal">
    <el-form ref="formData" :rules="rules" :model="formData" class="form-wrap" label-position="top" label-width="80px">
      <el-form-item :label="$t('configLazyLoad.process.nodeName')" prop="DisplayName">
        <el-input v-model.trim="formData.DisplayName"></el-input>
      </el-form-item>
      <el-form-item label="语音文件：">
        <el-select v-model="formData.Filename" placeholder="请选择" style="width: 100%;">
          <el-option
            label="请选择"
            value="">
          </el-option>
          <el-option
            v-for="item in ivr_sound.ivr_sound"
            v-if="item.SoundType == 'normal'"
            :label="item.DisplayName"
            :key="item._id"
            :value="item.Filename">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="呼叫变量：">
        <el-select v-model="formData.Variable" placeholder="请选择" style="width: 100%;">
          <el-option
            v-for="item in vars.vars"
            :key="item._id"
            :label="item.VarName"
            :value="item.VarName">
          </el-option>
        </el-select>
      </el-form-item>
      <div v-if="isShowRingTimg">
        <span class="label-span"><span>回呼振铃时长：</span></span>
        <el-form-item>
          <el-input v-model.trim="formData.CallRingTime" max='120' min='0' type='Number' @input="CallRingTimeInput">
            <template slot="append">秒</template>
          </el-input>
        </el-form-item>
        <el-form-item label="回呼失败后转向：">
          <node-select-item :curPbx="curPbx" :ToPeerType="formData.ToPeerTypeCallbackFailure" :ToPeer="formData.ToPeerCallbackFailure" ref="nodeSelect"></node-select-item>
        </el-form-item>
      </div>
    </el-form>
    <div class="footer">
      <el-button class='slide-box__button' @click='cancelClick'>
        取消
      </el-button>
      <el-button class='slide-box__button' type="primary" @click='saveClick'>
        保存
      </el-button>
    </div>
  </div>
</template>
<script>
  import nodeSelectItem from '../../base/nodeSelectItem.vue'
  import {deepClone} from 'src/utils/m7Utils.js'
  export default {
    name: 'webcallDeal',
    data () {
      return {
        formData: { // 双向回呼节点值
          _id: '',
          DisplayName: '',
          Filename: '',
          Variable: '',
          CallRingTime: 30,
          ToPeerTypeCallbackFailure: 'system',
          ToPeerCallbackFailure: '0'
        },
        rules: {
          DisplayName: [
            { required: true, message: this.$t('configLazyLoad.process.processLabel7'), trigger: 'blur' }
          ]
        },
        isShowRingTimg: false
      }
    },
    computed: {
      curPbx () {
        return this.$store.state.config.callCenter.currentPBX
      },
      Account () {
        return this.$store.state.session.user.account
      },
      ivr_sound () {
        return this.$store.state.session.ivrMap[this.curPbx].data
      },
      vars () {
        return this.$store.state.session.ivrMap[this.curPbx].data
      }
    },
    components: {
      nodeSelectItem
    },
    props: {
      form: {
        type: Object,
        default: function () {
          return {
            _id: '',
            DisplayName: '',
            MenuType: 'inner',
            NextPeerType: '',
            NextPeer: '',
            MenuOutNum: '',
            CallRingTime: 30,
            ToPeerTypeCallbackFailure: 'system',
            ToPeerCallbackFailure: '0'
          }
        }
      }
    },
    methods: {
      // 初始化表单
      initForm (obj) {
        this.formData = deepClone(obj)
        let flag = false
        if (this.ivr_sound.ivr_sound && this.ivr_sound.ivr_sound.length > 0) {
          this.ivr_sound.ivr_sound.forEach(item => {
            if (this.form.Filename === item.Filename) {
              flag = true
            }
          })
        }
        if (!flag) {
          this.formData.Filename = ''
        } else {
          this.formData.Filename = this.form.Filename
        }
      },
      cancelClick () {
        this.$emit('cancelClick')
      },
      // 更新pbx数据
      updateVuexPbx () {
        this.$store.dispatch('queryTwoWayCall', {Account: this.Account, PBX: this.curPbx}).then(res => {
          if (res && res.success) {
            this.$store.commit('SET_IVRMAP_DETAIL', {PBX: this.curPbx, typeName: 'ivr_node_twowaycall', data: res.list || [], flag: 'data', node: this})
          }
          this.$emit('saveClick')
        })
      },
      saveClick () {
        this.$refs['formData'].validate((valid) => {
          if (valid) {
            let form = deepClone(this.formData)
            if (this.isShowRingTimg) {
              form.ToPeerTypeCallbackFailure = this.$refs.nodeSelect.ToPeerTypeThis
              form.ToPeerCallbackFailure = this.$refs.nodeSelect.ToPeerThis
            }
            if (!form.id) {
              form.Account = this.Account
              form.PBX = this.curPbx
            }
            form.UpdateTime && delete form.UpdateTime
            this.$store.dispatch('updateTwoWayCall', form).then((res) => {
              if (res.success) {
                this.$message({
                  message: '保存成功',
                  type: 'success'
                })
                this.updateVuexPbx()
              } else {
                this.$message.error('保存失败')
              }
            })
          }
        })
      },
      CallRingTimeInput (value) {
        if (value <= 1) {
          this.formData.CallRingTime = 1
        } else if (value >=120) {
          this.formData.CallRingTime = 120
        }
      },
      showPbxVision () {
        this.$store.dispatch('getNewFeaturesReport', {pbx: this.curPbx}).then((res)=>{
          if (res.data.features && res.data.features.indexOf('twoWayCall_V2') > -1) {
            this.isShowRingTimg = true
          } else {
            this.isShowRingTimg = false
          }
        })
      }
    },
    beforeMount () {
      this.initForm(this.form)
    },
    watch: {
      form (cur, old) {
        // 重置表单
        this.$refs['formData'].resetFields()
        this.initForm(cur)
        this.showPbxVision()
      }
    }
  }
</script>
<style lang="stylus" scoped>
  @import "../../../../../../assets/common.styl"
  .label-span
    .tip
      color #FAA267
    display flex
    justify-content space-between
  .note
    margin-top 20px
    padding 0 20px
    p
      color $c-prompt
    p.two
      margin-top 10px
  .wrap_menu
    width 100%
    padding-bottom 68px
    .form-wrap
      padding 20px
  .footer
    position absolute
    bottom 0
    left 0
    width 100%
    background rgba(255,255,255,1)
    box-shadow 0px -2px 4px 0px rgba(5,49,38,0.05)
    border-top 1px solid rgba(232,232,232,1)
    display flex
    justify-content center
    padding 13px 0
    box-sizing border-box
    .slide-box__button
      padding 8px 20px 8px
      box-sizing border-box
</style>
<style lang="stylus">
  .ivrwebcalldeal
    .el-input-group--append
      margin-top 5px
</style>
